package com.dldata.devtools30.controller.zhibiao;

import com.alibaba.fastjson2.JSON;
import com.dldata.devtools30.entity.*;
import com.dldata.devtools30.repository.metadata.DevtModelMetadataTableDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbcfgInfoEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbclassInfoEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbplanInfoEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbplanItemEntityDao;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/DevtZbplanItemEntityController")
@Slf4j
public class DevtZbplanItemEntityController {

    @Autowired
    DevtZbplanItemEntityDao devtZbplanItemEntityDao;
    
    @Autowired
    DevtZbcfgInfoEntityDao devtZbcfgInfoEntityDao;
    
    @Autowired
    DevtModelMetadataTableDao devtModelMetadataTableDao;
    
    @Autowired
    DevtZbclassInfoEntityDao zbclassInfoEntityDao;
    
    @Autowired
    DevtZbplanInfoEntityDao devtZbplanInfoEntityDao;
    
    @RequestMapping(value = "/getAllZbToAdd")
    @ResponseBody
    public Result getAllZbToAdd(@RequestParam Map<String, String> paramMap){
    
        List<DevtZbclassInfoEntity> zbclassInfoEntityList = zbclassInfoEntityDao.getAll();
        Map<String, DevtZbclassInfoEntity> zbclassInfoEntityMap
                = zbclassInfoEntityList.stream().collect(Collectors.toMap(DevtZbclassInfoEntity::getId, v -> v));
    
        List<DevtZbcfgInfoEntity> zbcfgInfoEntityList = devtZbcfgInfoEntityDao.getAllByNotPlanId(paramMap.get("planId"));
    
        paramMap.getOrDefault("","");
        
        List<HashMap> hashMapList=new ArrayList<>();
        zbcfgInfoEntityList.stream().forEach(
            devtZbcfgInfoEntity -> {
                HashMap hashMap = new HashMap<>();
                hashMap.putAll(JSON.parseObject(JSON.toJSONString(devtZbcfgInfoEntity)));
                
                if(zbclassInfoEntityMap.containsKey(devtZbcfgInfoEntity.getZbclassid())){
                    DevtZbclassInfoEntity zbclassInfoEntity = zbclassInfoEntityMap.get(devtZbcfgInfoEntity.getZbclassid());
                    hashMap.put("zbclassname",zbclassInfoEntity.getZbclassname());
                }
                hashMapList.add(hashMap);
            }
        );
        return Result.ok(hashMapList);
    }
    
    
    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){
    
        List<DevtZbcfgInfoEntity> zbcfgInfoEntityList = devtZbcfgInfoEntityDao.getAllByPlanId(paramMap.get("planId"));
        Map<String, DevtZbcfgInfoEntity> zbcfgInfoMap = zbcfgInfoEntityList.stream().collect(Collectors.toMap(DevtZbcfgInfoEntity::getId,v->v));
    
        List<DevtModelMetadataTableEntity> metadataTableEntityList = devtModelMetadataTableDao.getAll();
        Map<String, DevtModelMetadataTableEntity> metadataTableMap
                = metadataTableEntityList.stream().collect(Collectors.toMap(DevtModelMetadataTableEntity::getId,v->v));
    
        List<DevtZbplanItemEntity> zbplanInfoEntityArrayList =devtZbplanItemEntityDao.getAllByPlanId(paramMap.get("planId"));
        List<HashMap> hashMapList=new ArrayList<>();
        zbplanInfoEntityArrayList.stream().forEach(
            devtZbplanItemEntity -> {
                HashMap hashMap = new HashMap<>();
                hashMap.putAll(JSON.parseObject(JSON.toJSONString(devtZbplanItemEntity)));
                hashMapList.add(hashMap);
    
                DevtZbcfgInfoEntity infoEntity = zbcfgInfoMap.get(devtZbplanItemEntity.getPlanzbid());
                if(infoEntity!=null){
                    DevtModelMetadataTableEntity metadataTableEntity = metadataTableMap.get(infoEntity.getModelid());
                    hashMap.put("tablecode",metadataTableEntity.getCode());
                    hashMap.put("tablename",metadataTableEntity.getName());
                }
            }
        );
        
        return Result.ok(hashMapList);
    }
    
    @RequestMapping(value = "/saveSelectZb")
    @ResponseBody
    public Result saveSelectZb(@RequestParam Map<String, String> paramMap){
        String planid=paramMap.get("planid");
        String zbidStr=paramMap.get("zbidStr");
        String[] zbIdAry = zbidStr.split(",");
        DevtZbplanInfoEntity zbplanInfoEntity = devtZbplanInfoEntityDao.findById(planid).orElse(new DevtZbplanInfoEntity());
    
        List<DevtZbplanItemEntity> allByPlanId = devtZbplanItemEntityDao.getAllByPlanId(planid);
        for (int i = 0; i < allByPlanId.size(); i++) {
            DevtZbplanItemEntity devtZbplanItemEntity = allByPlanId.get(i);
            devtZbplanItemEntity.setXh(i+1);
        }
    
        for (int i = 0; i < zbIdAry.length; i++) {
            String zbid = zbIdAry[i];
            DevtZbplanItemEntity devtZbplanItemEntity=new DevtZbplanItemEntity();
            devtZbplanItemEntity.setId(UUID.randomUUID().toString());
            devtZbplanItemEntity.setPlancode(zbplanInfoEntity.getPlancode());
            devtZbplanItemEntity.setPlanid(zbplanInfoEntity.getId());
    
            DevtZbcfgInfoEntity infoEntityDaoById = devtZbcfgInfoEntityDao.findById(zbid).orElse(new DevtZbcfgInfoEntity());
            devtZbplanItemEntity.setPlanzbcode(infoEntityDaoById.getZbcode());
            devtZbplanItemEntity.setPlanzbid(zbid);
            devtZbplanItemEntity.setPlanzbname(infoEntityDaoById.getZbname());
            
            devtZbplanItemEntity.setXh(allByPlanId.size()+i+1);
            devtZbplanItemEntityDao.save(devtZbplanItemEntity);
        }
        
    
        return Result.ok("保存成功");
    }
    
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam Map<String, String> paramMap){
        
        String id =paramMap.get("id");
        if(StringUtil.isEmptyOrLength0(id)){
            return  Result.error("参数有误");
        }
        try {
            DevtZbplanItemEntity devtZbplanItemEntity = devtZbplanItemEntityDao.findById(id).orElse(null);
            if(devtZbplanItemEntity!=null){
                devtZbplanItemEntityDao.delete(devtZbplanItemEntity);
            }
        }
        catch (Exception ex){
            log.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return  Result.error("操作错误，请稍候再试!");
        }
        return  Result.ok("删除成功");
    }
    
    @RequestMapping("toSelectZb")
    public String toSelectZb(String planid, Model model){
        model.addAttribute("planid",planid);
        return "thymeleaf/devtools30/zhibiao/v_zhibiaoplanSelectZbList";
    }
}
